/*
 * Content: 继承Thread类创建线程、实现Runnable接口创建线程、同一实例创建线程
 * --------------------------------------------------
 * java 多线程: https://juejin.cn/post/6844903591044448270
 */

public class java_1100_thread {
    public static void main(String[] args) {

        // 继承Thread类创建线程
        System.out.println("--------------------继承Thread类创建线程--------------------");

        // 创建线程
        MyThread1 thread1 = new MyThread1();
        MyThread1 thread2 = new MyThread1();

        // 启动线程
        thread1.start();
        thread2.start();

        // 实现Runnable接口创建线程
        System.out.println("--------------------实现Runnable接口创建线程--------------------");

        // 创建线程
        Thread thread3 = new Thread(new MyThread2());
        Thread thread4 = new Thread(new MyThread2());

        // 启动线程
        thread3.start();
        thread4.start();

        // Runnable: 同一个实例创建两个线程: 有这种创建的需求吗？什么场景下需要这么创建？
        System.out.println("--------------------实现Runnable接口创建线程: 同一实例创建线程--------------------");

        MyThread2 myThread2 = new MyThread2();
        Thread thread6 = new Thread(myThread2);   // 这两个线程是使用同一个实例创建的
        Thread thread7 = new Thread(myThread2);

        // 启动线程
        thread6.start();
        thread7.start();
    }
}

// 创建线程方式一
class MyThread1 extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 10000000; ++i) {
            System.out.println("Thread1-(Thread): " + i);
        }
    }
}

// 创建线程方式一
class MyThread2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 10000000; ++i) {
            System.out.println("Thread2-(Runnable): " + i);
        }
    }
}